package org.web3j;

import com.alibaba.fastjson.JSON;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.admin.Admin;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.ClientTransactionManager;
import org.web3j.tx.Contract;
import org.web3j.tx.TransactionManager;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class InvokeContract extends ClassLoader{
    private static String javaGenPath ="D:/wac/D_/wac/";

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, MalformedURLException, ClassNotFoundException {
        Web3j web3j = Web3j.build(new HttpService("http://116.62.138.108:8545"));
        Admin admin =Admin.build(new HttpService("http://116.62.138.108:8545"));
        String deployeraccount ="0x72769902d04063004a97d1deab35778184d6c626";
        //String contractAddress ="0x7c4a0404b5ebc3e21ff18c6e6b0badba06c3aaada2169cf6fed03d1a43b4f3de";
        String contractAddress ="0x076d55e1a5e16ce1033ff49207fd098677fcda12";//hash:0xd4af29345a3d40b12811dba87f4dd228e9b28adbcb198428d160ba90efb10ddd
        ClientTransactionManager transactionManager = new ClientTransactionManager(web3j,deployeraccount);

        InvokeContract invokeContract = new InvokeContract();
        Class clazz = invokeContract.findClass();
       /* ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();

        // This URL for a directory will be searched *recursively*
        File file =new File(javaGenPath+"com/qkltong/wallet/web3j/contract/target/classes/com/qkltong/wallet/web3j/contract");

            ClassLoader custom =
                    new URLClassLoader( new URL[] { file.toURI().toURL() } );

            // this class should be loaded from your directory;com.qkltong.wallet.web3j.contract.
            Class< ? > clazz = custom.loadClass( "com.qkltong.wallet.web3j.contract.Template_sol_TokenERC20" );*/

            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                System.out.println(method.getName());
            }
           // Method[]  methods =    clazz.getDeclaredMethods();
        Method loadMethod =clazz.getDeclaredMethod("load",String.class, Web3j.class, TransactionManager.class, BigInteger.class, BigInteger.class);
       //Constructor constructor = clazz.getConstructor(String.class, Web3j.class, TransactionManager.class, BigInteger.class, BigInteger.class);

        Object  ooooo=  loadMethod.invoke(null,new Object[]{contractAddress,web3j,transactionManager,new BigInteger("21000"), Contract.GAS_LIMIT});
        System.out.println("contract:"+ JSON.toJSONString(ooooo));
    }
    Class findClass(){
        byte[] cLassBytes = null;
        Path path;
        try {
            path = Paths.get(new File(javaGenPath+"com/qkltong/wallet/web3j/contract/target/classes/com/qkltong/wallet/web3j/contract/Template_sol_TokenERC20.class").toURI());
            cLassBytes = Files.readAllBytes(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Class cLass = defineClass("com.qkltong.wallet.web3j.contract.Template_sol_TokenERC20", cLassBytes, 0, cLassBytes.length);
        return cLass;
      // return null;
    }
}
